package com.person.mall.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.person.mall.dao.ProductMapper;
import com.person.mall.enums.ProductDetailEnum;
import com.person.mall.enums.ResponseEnum;
import com.person.mall.pojo.Product;
import com.person.mall.service.ICategoryService;
import com.person.mall.service.IProductService;
import com.person.mall.vo.ProductDetailVo;
import com.person.mall.vo.ProductVo;
import com.person.mall.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.person.mall.enums.ProductDetailEnum.DELETE;
import static com.person.mall.enums.ProductDetailEnum.OFF_SALE;
import static com.person.mall.enums.ResponseEnum.PRODUCT_OFF_ERROR;

/**
 * @author 最酷的桃儿wjc
 * * @date 2022/5/17
 */
@Service
@Slf4j
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ICategoryService iCategoryService;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public ResponseVo<PageInfo> productList(Integer categoryId, Integer pageNum, Integer page) {

        Set<Integer> categoryIdSet = new HashSet<>();
        if (categoryId != null) {
            iCategoryService.findSubCategoryId(categoryId, categoryIdSet);
            categoryIdSet.add(categoryId);
        }
        //这里就是是使用PageHelper
        PageHelper.startPage(pageNum,page);
        List<Product> productList = productMapper.selectByCategoryIdSet(categoryIdSet);
        List<ProductVo> productVoList = productList.stream()
                .map(e -> {
                    ProductVo productVo = new ProductVo();
                    BeanUtils.copyProperties(e, productVo);
                    return productVo;
                })
                .collect(Collectors.toList());
        //pageInfo当中的信息pageHelper也提供好了
        PageInfo pageInfo = new PageInfo<>(productList);
        pageInfo.setList(productVoList);

//        List<Product> productList = productMapper.selectByCategoryIdSet(categoryIdSet);
//        List<ProductVo> productVoList = new ArrayList<>();
//        for (Product product : productList) {
//            ProductVo productVo = new ProductVo();
//            BeanUtils.copyProperties(product,productVo);
//            productVoList.add(productVo);
//        }

        return ResponseVo.successGetData(pageInfo);
    }

    @Override
    public ResponseVo<ProductDetailVo> productDetail(Integer productId) {
        Product product = productMapper.selectByPrimaryKey(productId);
        //只对确定性的条件进行判断
        if (product.getStatus().equals(OFF_SALE.getCode()) || product.getStatus().equals(DELETE.getCode())){
            return ResponseVo.error(PRODUCT_OFF_ERROR);
        }
        ProductDetailVo productDetailVo = new ProductDetailVo();
        BeanUtils.copyProperties(product,productDetailVo);
        return ResponseVo.successGetData(productDetailVo);
    }
}
